package com.dave.common.dao.impl;

import com.dave.common.constants.DaoConstants;
import com.dave.common.dao.BaseDao;
import com.dave.common.entity.BaseEntity;
import com.dave.common.exception.SystemException;
import com.dave.common.utils.BeanUtils;
import com.github.pagehelper.Page;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @PageName com.dave.common.dao.impl
 * @Description Dao基础类实现
 * @Author 何全裕
 * @Create 2017-07-30 20:43
 * @Version 1.0
 **/
public class BaseDaoImpl<T extends BaseEntity, PK extends Serializable> implements BaseDao<T,PK>{
    private Logger logger = LoggerFactory.getLogger(BaseDaoImpl.class);

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    private final String SQLNAME_SEPARATOR = ".";

    private String sqlMapperNamespace = getDefaultSqlMapperNamespace();

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    /**
     * 获取泛型类型的实体对象类全名
     *
     * @return
     */
    private String getDefaultSqlMapperNamespace() {
        Class<?> genericClass = BeanUtils.getGenericClass(this.getClass());
        return genericClass == null ? null : genericClass.getName();
    }

    /**
     * 将sqlMapperNamespace与给定的sqlName组合在一起.
     *
     * @param sqlName
     * @return
     */
    protected String getSqlName(String sqlName) {
        return sqlMapperNamespace + SQLNAME_SEPARATOR + sqlName;
    }

    @Override
    public T find(T entity) {
        try {
            Assert.notNull(entity);
            return sqlSessionTemplate.selectOne(getSqlName(DaoConstants.SQLNAME_FIND), entity);
        } catch (Exception e) {
            logger.error(String.format("查询对象出错！语句：%s", getSqlName(DaoConstants.SQLNAME_FIND)), e);
            throw new SystemException(String.format("查询对象出错！语句：%s", getSqlName(DaoConstants.SQLNAME_FIND)), e);
        }
    }

    @Override
    public T findById(PK id) {
        try {
            logger.info(String.format("查询对象语句：%s", getSqlName(DaoConstants.SQLNAME_FIND_BY_ID)));
            logger.info(sqlSessionTemplate+"是的是的是的是的是的");
            return sqlSessionTemplate.selectOne(getSqlName(DaoConstants.SQLNAME_FIND_BY_ID), id);
        } catch (Exception e) {
            logger.error(String.format("查询对象出错！语句：%s", getSqlName(DaoConstants.SQLNAME_FIND_BY_ID)), e);
            throw new SystemException(String.format("查询对象出错！语句：%s", getSqlName(DaoConstants.SQLNAME_FIND_BY_ID)), e);
        }
    }

    @Override
    public List<T> findByIds(List<PK> PK) {
        return null;
    }

    @Override
    public List<T> findList(T entity) {
        return null;
    }

    @Override
    public List<T> findByPage(Map<String, Object> params, Page<T> page) {
        return null;
    }

    @Override
    public List<T> findAll() {
        return null;
    }

    @Override
    public Long findCount() {
        return null;
    }

    @Override
    public Long findCount(T entity) {
        return null;
    }

    @Override
    public PK insert(T entity) {
        return null;
    }

    @Override
    public PK delete(PK id) {
        return null;
    }

    @Override
    public PK delete(List<PK> ids) {
        return null;
    }

    @Override
    public PK deleteAll() {
        return null;
    }

    @Override
    public PK update(T entity) {
        return null;
    }

    @Override
    public boolean check(Map<String, Object> params) {
        return false;
    }

    @Override
    public void batchInsert(List<T> entitys) {

    }

    @Override
    public void batchUpdate(List<T> entitys) {

    }
}
